package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.common.utils.TrendUtil;
import com.kaizzi.data.jdl.been.TrendBeen;
import com.kaizzi.data.jdl.model.CommentsModel;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page7Model;
import com.kaizzi.data.jdl.service.*;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description page 7 data
 * @date 2020/9/11
 */
@Component
public class PageData7 {

    @Resource
    private CrmMemberService crmMemberService;
    @Resource
    private MotionClocksService motionClocksService;
    @Resource
    private TeamMemberService teamMemberService;
    @Resource
    private TaskLogsService taskLogsService;
    @Resource
    private OrderListService orderListService;

    public String getData(ExcelModel model){

        List<Page7Model> page7s = model.getPage7s();
        List<CommentsModel> comments = model.getComments();

        List<TempPage7> pageList = new ArrayList<>();
        Page7 page7 = new Page7();

        for (CommentsModel comment:comments) {
            if (comment.getPage().equals("P7")){
                if (comment.getName().equals("title")){
                    page7.setTitle(comment.getData());
                }
                if (comment.getName().equals("近期活动")){
                    page7.setRecentActivity(comment.getData());
                }
                if (comment.getName().equals("近期更新")){
                    page7.setRecentUpdate(comment.getData());
                }
            }
        }
        for (Page7Model page7Model:page7s) {
            String date = page7Model.getDate();
            List<String> dateList= DateUtil.getTransTime(date);//根据表格内日期 转为 list


            TempPage7 tempPage7 = new TempPage7();
            tempPage7.setDate(new TrendBeen(DateUtil.getTransTime4(page7Model.getDate()),0));
            tempPage7.setUv(new TrendBeen(StringUtil.pointFormat(Long.parseLong(page7Model.getUv().replaceAll(",",""))),0));
            tempPage7.setPv(new TrendBeen(StringUtil.pointFormat(Long.parseLong(page7Model.getPv().replaceAll(",",""))),0));
            long totalUser = crmMemberService.getUserCount(null, dateList.get(1),"");
            long newUser = crmMemberService.getUserCount(dateList.get(0), dateList.get(1),"");
            tempPage7.setTotalUser(new TrendBeen(StringUtil.pointFormat(totalUser),0));
            tempPage7.setNewUser(new TrendBeen(StringUtil.pointFormat(newUser),0));
            String newVisitUser = page7Model.getNewVisitUser().replaceAll(",", "");
            tempPage7.setNewVisitUser(new TrendBeen(StringUtil.pointFormat(Long.parseLong(newVisitUser)),0));
//          跳出率（新访问人数 - 新增用户数 / 新访问人数）
            tempPage7.setJumpOutRate(new TrendBeen(getJumpRate(newUser,newVisitUser),0));
            tempPage7.setActiveUser(new TrendBeen(StringUtil.pointFormat(Long.parseLong(page7Model.getActiveUser().replaceAll(",",""))),0));
            tempPage7.setAppShareUser(new TrendBeen(StringUtil.pointFormat(Long.parseLong(page7Model.getShareUser().replaceAll(",",""))),0));
            tempPage7.setAppShareCount(new TrendBeen(StringUtil.pointFormat(Long.parseLong(page7Model.getShareCount().replaceAll(",",""))),0));
            tempPage7.setClockInCount(new TrendBeen(StringUtil.pointFormat(motionClocksService.getClockInCount(dateList.get(0),dateList.get(1))),0));

            long clockInUser = motionClocksService.getClockInUser(dateList.get(0), dateList.get(1));
            tempPage7.setClockInUser(new TrendBeen(StringUtil.pointFormat(clockInUser),0));
            tempPage7.setCreateTeamUser(new TrendBeen(StringUtil.pointFormat(teamMemberService.getTeamUser(dateList.get(0),dateList.get(1),"1")),0));
            tempPage7.setJoinTeamUser(new TrendBeen(StringUtil.pointFormat(teamMemberService.getTeamUser(dateList.get(0),dateList.get(1),"0")),0));
            // TODO: 2020/11/16 积分
            tempPage7.setExpendPoint(new TrendBeen(StringUtil.pointFormat(taskLogsService.getExpendPoints(dateList.get(0),dateList.get(1))),0));
//            tempPage7.setExpendPoint(new TrendBeen("0",0));
            tempPage7.setGetPoint(new TrendBeen(StringUtil.pointFormat(taskLogsService.getPoints(dateList.get(0),dateList.get(1))),0));
//            tempPage7.setGetPoint(new TrendBeen("0",0));
            tempPage7.setOrderUser(new TrendBeen(StringUtil.pointFormat(orderListService.getOrderUser(dateList.get(0),dateList.get(1))),0));
            tempPage7.setExchangeUser(new TrendBeen(StringUtil.pointFormat(orderListService.getExchangeUser(dateList.get(0),dateList.get(1))),0));

            pageList.add(tempPage7);
        }

        TrendUtil.trendSetting(pageList);
        page7.setDataPeriod(DateUtil.getLastWeekDate());
        page7.setTempPage7s(pageList);

        return JsonUtil.serialize(page7);
    }

    /**
     * @param newUser 新增用户数
     * @param newVisitUser 新访问人数
     * @return 跳出率
     */
    private static String getJumpRate(long newUser,String newVisitUser){
        double rate = (Double.parseDouble(newVisitUser) - newUser)/Long.parseLong(newVisitUser) * 100 ;
        return StringUtil.decimalFormat(rate);
    }


    @Data
    private static class TempPage7 {
        /**
         * 日期
         */
        private TrendBeen date;
        /**
         * 累计uv
         */
        private TrendBeen uv;
        /**
         * 当周累计pv
         */
        private TrendBeen pv;
        /**
         * 总用户数
         */
        private TrendBeen totalUser;
        /**
         * 新增用户数
         */
        private TrendBeen newUser;
        /**
         * 新访问人数
         */
        private TrendBeen newVisitUser;
        /**
         * 跳出率 （新访问人数-新增用户数/新访问人数）
         */
        private TrendBeen jumpOutRate;
        /**
         * 活跃用户数
         */
        private TrendBeen activeUser;
        /**
         * 小程序分享人数
         */
        private TrendBeen appShareUser;
        /**
         * 小程序分享次数
         */
        private TrendBeen appShareCount;
        /**
         * 打卡次数
         */
        private TrendBeen clockInCount;
        /**
         * 打卡人次
         */
        private TrendBeen clockInUser;
        /**
         * 发起组队人数
         */
        private TrendBeen createTeamUser;
        /**
         * 参与组队人数
         */
        private TrendBeen joinTeamUser;
        /**
         * 积分消耗
         */
        private TrendBeen expendPoint;
        /**
         * 积分获取
         */
        private TrendBeen getPoint;
        /**
         * 现金商城下单人数
         */
        private TrendBeen orderUser;
        /**
         * 兑换人数
         */
        private TrendBeen exchangeUser;
    }

    @Data
    private static class Page7 {
        /**
         * 周日期
         */
        private List<TempPage7> tempPage7s;
        /**
         * 近期活动
         */
        private String recentActivity;
        /**
         * 近期更新
         */
        private String recentUpdate;
        /**
         * 标题
         */
        private String title;
        /**
         * Data Period
         */
        private String dataPeriod;
    }

}
